<?php

/**
 * 用户登录记录管理
 *
 * @author: zangyilei
 * @date: 2017年05月04日
 */

namespace Qy\Controller;

use Common\Controller\BaseController;

class LoginRecordController extends BaseController {

    /**
     * 记录新增
     */
    public function insert() {
        $res = $this->is_exist();

        if (empty($res) && !empty($this->iCompanyId)) {
            $loginRecord = D("LoginRecord");
            $loginRecord->user_id = $this->iUserId;
            $loginRecord->company_id = $this->iCompanyId;
            $loginRecord->create_time = format_time();
            $loginRecord->add();
        }
    }

    /**
     * 记录查询
     */
    protected function is_exist() {
        $user_id = $this->iUserId;
        $company_id = $this->iCompanyId;
        $where['user_id'] = $user_id;
        $where['company_id'] = $company_id;

        $res = D("LoginRecord")->where("create_time >= date(now()) and create_time < DATE_ADD(date(now()),INTERVAL 1 DAY)")->where($where)->count(1);

        return $res;
    }

}
